import { store } from "@/store";
import { defineStore } from "pinia";
import ExamineAPI, {
  type ExamineListQuery,
  type ExamineItemType,
  type ExamineForm,
} from "@/api/agent/examine";

export const useExamineStore = defineStore("agent-examine", () => {
  const list = ref<ExamineItemType[]>([]);

  const totalCount = ref(0);

  const totalPage = ref(0);

  const loading = reactive({
    list: false,
    create: false,
    update: false,
    delete: false,
    examine: false,
  });

  async function fetchList(params: ExamineListQuery) {
    loading.list = true;
    const response = await ExamineAPI.getExamineList(params);
    loading.list = false;
    if (response) {
      list.value = response.records;
      totalCount.value = response.total_count;
      totalPage.value = response.total_page;
    }
  }

  async function fetchExamine(data: ExamineForm) {
    loading.examine = true;
    await ExamineAPI.examineApply(data);
    loading.examine = false;
  }

  return {
    list,
    totalCount,
    totalPage,
    loading,
    fetchList,
    fetchExamine,
  };
});

export const useExamineStoreHook = () => {
  return useExamineStore(store);
};
